﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ADiKt {
    class WormMenuScreen : MenuScreen {
        Worm worm;
        Human humanWorm;
        AI aiWorm;

        SpinEntry typeME;
        MenuEntry leftME;
        MenuEntry rightME;
        SpinEntry difficultyME;
        SpinEntry colourME;
        MenuEntry backME;

        List<string> colourNames = new List<string>(Globals.colListFree.Values);
        List<Color> colourValues = new List<Color>(Globals.colListFree.Keys);
        static int currentColour = 0;

        public WormMenuScreen(Worm worm) : base("Edit Line Details") {
            this.worm = worm;

            colourME = new SpinEntry("Colour: " + worm.colourName);
            backME = new MenuEntry("Back");

            if (worm.GetType() == typeof(Human)) {
                humanWorm = (Human)worm;
                typeME = new SpinEntry("Type: Human");
                leftME = new MenuEntry("Left: " + humanWorm.left.ToString());
                rightME = new MenuEntry("Right: " + humanWorm.right.ToString());
            } else {
                typeME = new SpinEntry("Type: AI");
                aiWorm = (AI)worm;
                difficultyME = new SpinEntry("Difficulty: ", Globals.difficulty, aiWorm.difficulty, delegate() { aiWorm.difficulty = (int)difficultyME.value;});
            }

            // Event hooks ----------------------------------------------------------------
            colourME.Next += ColourNext;
            colourME.Last += ColourLast;
            typeME.Next += changeType;
            typeME.Last += changeType;
            backME.Selected += OnCancel;

            //add entries -----------------------------------------------------------------
            MenuEntries.Add(typeME);
            MenuEntries.Add(colourME);
            if (worm.GetType() == typeof(Human)) {
                MenuEntries.Add(leftME);
                MenuEntries.Add(rightME);
            }else{
                MenuEntries.Add(difficultyME);
            }
            MenuEntries.Add(backME);
        }

        // Event hooks ----------------------------------------------------------------
        void ColourNext(object sender) {
            currentColour = (currentColour + 1) % Globals.colListFree.Count;
            colourME.Text = "Colour: " + colourNames[currentColour];
            worm.changeColour(colourValues[currentColour]);
            colourNames = new List<string>(Globals.colListFree.Values);
        }

        void ColourLast(object sender) {
            currentColour = (Globals.colListFree.Count + currentColour - 1) % Globals.colListFree.Count;
            colourME.Text = "Colour: " + colourNames[currentColour];
            worm.changeColour(colourValues[currentColour]);
            colourNames = new List<string>(Globals.colListFree.Values);
        }

        void changeType(object sender) {
            worm.delete();
            if (worm.GetType() == typeof(Human))
                worm = new AI(worm.colour);
            else
                worm = new Human(Keys.Q, Keys.W, worm.colour);
            this.ExitScreen();
            ScreenManager.AddScreen(new WormMenuScreen(this.worm), null);
        }

        protected override void OnCancel(PlayerIndex playerIndex) {
            this.ExitScreen();
            ScreenManager.AddScreen(new SetupScreen(),playerIndex);
        }

    }
}
